﻿using Bend.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;

namespace Flame.Dlr
{
    [Flame.Base.DlrPlugin]
    public static class DlrPlugin
    {
        public static void Load()
        {
            Languages.Set(typeof(HttpServerExec), "HttpServer", "http");
        }
    }

    class HttpServerExec : IExecutable
    {
        HttpServer_WithEvent _httpServer;
        string _text = null;
        string _responsetext = null;
        int _timeout = 0;

        public HttpServerExec() : this("127.0.0.1", 8080)
        {
        }
        public HttpServerExec(string localAdress = "127.0.0.1",int port = 8080)
        {
            _httpServer = new HttpServer_WithEvent(IPAddress.Parse(localAdress), port);
            _httpServer.handleGETRequested += _httpServer_handleGETRequested;
            _httpServer.handlePOSTRequested += _httpServer_handlePOSTRequest;
            new Thread(new ThreadStart(_httpServer.listen)).Start();
        }

        void _httpServer_handleRequest(HttpProcessor p)
        {
            if (_text != null)
            {
                p.writeSuccess();
                p.outputStream.Write(_text);
                _text = null;
            }
            else if (_responsetext == null)
            {
                p.writeSuccess();

                _responsetext = Uri.UnescapeDataString(p.http_url.Substring(1));

                p.writeSuccess();
                p.outputStream.Write("");
                //p.outputStream.Write(_text);
                //_text = null;
            }
            else
            {
                p.writeSuccess();
                p.outputStream.Write("");
            }
        }

        void _httpServer_handlePOSTRequest(object sender, HttpServer_WithEvent.HandledRequestEventArgs e)
        {
           _httpServer_handleRequest( e.HttpProcessor);
        }

        void _httpServer_handleGETRequested(object sender, HttpServer_WithEvent.HandledRequestEventArgs e)
        {
            _httpServer_handleRequest(e.HttpProcessor);
        }

        public void ClearScope(ScopeParameters scopeParameters)
        {
        }

        private bool isTimeout()
        {
            return _timeout > 100;
        }

        public Result Execute(string text)
        {
            _timeout = 0;
            _text = text;
            while (_responsetext == null &&  !isTimeout())
            {
                Thread.Sleep(100);
                _timeout++;
            }

            if (_responsetext == null) return new Result() { Data = "TIMEOUT" };

            Result r = new Result() { Data = _responsetext }; 

            _responsetext = null;
            return r;
        }

        public ResultAssembly AddAssembly(AssemblyWrapper res)
        {
            return null;
        }

        public void AddVariable(Variable variable)
        {
        }

        public Reflection.IIntellisense Intellisense
        {
            get { return null; }
        }
    }
}
